﻿using MyBookReadsList.Commands;
using MyBookReadsList.Data;
using MyBookReadsList.Helpers;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Controls;
using System.Windows.Input;

namespace MyBookReadsList.ViewModels
{
    public class LoginRegisterFormViewModel : ViewModelBase, IPageViewModel
    {
        private string message;
        private ICommand loginCommand;
        private ICommand registerCommand;
        private const int MinPasswordLength = 6;
        private const int MaxPasswordLength = 30;

        public string Name
        {
            get
            {
                return "Login Form";
            }
        }

        public string username { get; set; }

        public string Email { get; set; }

        //public string Message
        //{
        //    get
        //    {
        //        return this.message;
        //    }
        //    set
        //    {
        //        this.message = value;
        //        this.OnPropertyChanged("Message");
        //    }
        //}

        public ICommand Login
        {
            get
            {
                if (this.loginCommand == null)
                {
                    this.loginCommand = new RelayCommand(this.HandleLoginCommand);
                }
                return this.loginCommand;
            }
        }

        public ICommand Register
        {
            get
            {
                if (this.registerCommand == null)
                {
                    this.registerCommand = new RelayCommand(this.HandleRegisterCommand);
                }
                return this.registerCommand;
            }
        }

        public event EventHandler<LoginSuccessArgs> LoginSuccess;

        public LoginRegisterFormViewModel()
        {
            //this.Username = "DonchoMinkov222";
            //this.Email = "Minkov@doncho.com";
        }

        private void HandleRegisterCommand(object parameter)
        {
            var passwordBox = parameter as PasswordBox;
            var password = passwordBox.Password;

            if (password.Length < MinPasswordLength)
            {
                throw new ArgumentOutOfRangeException(
                    string.Format("Password must be at least {0} characters long",
                    MinPasswordLength));
            }
            else if (password.Length > MaxPasswordLength)
            {
                throw new ArgumentOutOfRangeException(
                    string.Format("Password must be at least {0} characters long",
                    MaxPasswordLength));
            }
            var authenticationCode = CalculateSHA1(password, Encoding.UTF8);

            DataPersister.RegisterUser(this.username, this.Email, authenticationCode);
            this.HandleLoginCommand(parameter);
        }

        private void HandleLoginCommand(object parameter)
        {
            var passwordBox = parameter as PasswordBox;
            var password = passwordBox.Password;

            var authenticationCode = CalculateSHA1(password, Encoding.UTF8);

            var sessionKey = DataPersister.LoginUser(this.username, authenticationCode);

            if (!string.IsNullOrEmpty(sessionKey))
            {
                this.RaiseLoginSuccess(this.username);
            }
        }

        public static string CalculateSHA1(string text, Encoding enc)
        {
            // Convert the input string to a byte array
            byte[] buffer = enc.GetBytes(text);

            // In doing your test, you won't want to re-initialize like this every time you test a
            // string.
            SHA1CryptoServiceProvider cryptoTransformSHA1 =
                new SHA1CryptoServiceProvider();

            // The replace won't be necessary for your tests so long as you are consistent in what
            // you compare.    
            string hash = BitConverter.ToString(
                cryptoTransformSHA1.ComputeHash(buffer)).Replace("-", "");

            return hash;
        }


        protected void RaiseLoginSuccess(string username)
        {
            if (this.LoginSuccess != null)
            {
                this.LoginSuccess(this, new LoginSuccessArgs(username));
            }
        }
    }
}